package com.wowangz.cms.systemmanage.preferential.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.Validate;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wowangz.cms.common.utils.SeriesGenerater;
import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.common.utils.UUIDGenerator;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.service.BaseService;
import com.wowangz.cms.systemmanage.country.pojo.Country;
import com.wowangz.cms.systemmanage.datadictionary.entity.CfgDataDictValue;
import com.wowangz.cms.systemmanage.datadictionary.service.CfgDataDictService;
import com.wowangz.cms.systemmanage.preferential.entity.TblPreferentialCode;

public class TblPreferentialCodeService extends BaseService {

	private Logger logger = LoggerFactory.getLogger(TblPreferentialCodeService.class);

	/**
	 * 分页查询总条数
	 * 
	 * @param map
	 * @return
	 */
	public long findPreferentialCodeCount(HashMap<String, Object> map) throws ServiceException {
		Long countNum = this.getSqlSession().selectOne("TblPreferentialCode.queryTblPreferentialCodeCount", map);
		return countNum;
	}

	/**
	 * 查询记录集合
	 * 
	 * @param map
	 * @return
	 */
	public List<TblPreferentialCode> findPreferentialCodeList(HashMap<String, Object> map) throws ServiceException {
		List<TblPreferentialCode> list = this.getSqlSession().selectList("TblPreferentialCode.queryTblPreferentialCodePage", map);
		return list;
	}
	
	/**
	 * 新增用户优惠
	 * @param province
	 * @throws Exception
	 */
	public String addPreferentialCode(TblPreferentialCode preferentialCode) throws ServiceException {
		this.getSqlSession().insert("TblPreferentialCode.insertPreferentialCode", preferentialCode);
		return preferentialCode.getCode();
	}
	/**
	 * 修改用户优惠
	 * @Title: updatePreferentialCode 
	 * @Description: 
	 * @return void
	 * @author Green.Long
	 * @date 2016年8月24日 下午2:49:13
	 */
	public void updatePreferentialCode(TblPreferentialCode preferentialCode) throws Exception {
		this.getSqlSession().update("TblPreferentialCode.updatePreferentialCode", preferentialCode);
	}
	
	/**
	 * 验证信息
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public boolean validationPreferentialCode(HashMap<String, Object> map) throws ServiceException {
		Long countNum = this.getSqlSession().selectOne("TblPreferentialCode.validationPreferentialCode", map);
		if(countNum != 0){
			return false;
		}else{
			return true;
		}
	}
	
	/**
	 * 批量新增优惠码（默认1年的有效期）
	 * @Title: addPreferentialCodeList 
	 * @Description: 
	 * @return void
	 * @author Green.Long
	 * @date 2016年8月24日 下午12:21:33
	 */
	public List<String> addPreferentialCodeList(TblPreferentialCode preferentialCode) throws ServiceException {
		DateTime now = new DateTime();
		Integer num = preferentialCode.getNum();	
		Validate.notNull(num, "优惠码个数不能为空");
		Validate.exclusiveBetween(0, 1001, num, String.format("每次只能生成1~1000个优惠码: %s", num));		
		preferentialCode.setDeadlineTime(now.plusYears(1).withTimeAtStartOfDay().toDate());
		preferentialCode.setDataStatus("InUse");
		List<String> codeList = new ArrayList<>();
		for(int i = 1; i <= num; ++i) {
			preferentialCode.setCode(generateCode(preferentialCode));
			preferentialCode.setId(UUIDGenerator.getUUID());	
			preferentialCode.setCreateTime(now.toDate());
			String code = addPreferentialCode(preferentialCode);
			codeList.add(code);
		}	
		return codeList;
	}
	
	/**
	 * 随机生成优惠码code并且校验
	 * @Title: generateCode 
	 * @Description: 
	 * @return String
	 * @author Green.Long
	 * @date 2016年8月24日 下午1:11:45
	 */
	public String generateCode(TblPreferentialCode preferentialCode) {
		CfgDataDictService cfgDataDictService =  new CfgDataDictService();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("value", preferentialCode.getChannel());
		List<CfgDataDictValue> cfgDataDictList = cfgDataDictService.findCfgDataDictListByType(map);
		CfgDataDictValue cfgDataDictValue = cfgDataDictList.get(0);
		int longName = SysConstant.PreferentialType.getLongName(preferentialCode.getPreferentialType());
		String code = (cfgDataDictValue.getDataValue() + longName + SeriesGenerater.randomLetterNum(6));
		while(true) {
			map.put("code", code);
			boolean validation = validationPreferentialCode(map);
			if(validation == true) {
				break;
			}
		}	
		return code;
	}
}
